/**
 * 
 */
package com.tpl.core.basedata.action.sys;

import java.util.List;

import com.tpl.core.JsonUtil;
import com.tpl.core.action.BaseAction;
import com.tpl.core.basedata.common.constant.ICacheConstants;
import com.tpl.core.basedata.service.DeptUserService;
import com.tpl.core.basedata.service.RoleService;
import com.tpl.core.common.page.Page;
import com.tpl.core.common.redis.RedisClientUtil;
import com.tpl.core.entity.sys.Role;
import com.tpl.core.entity.sys.User;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author 狄敏
 *
 * 创建时间：Jul 31, 2013 9:12:45 AM
 */
@Controller
@RequestMapping("/userRole")
public class UserRoleAction extends BaseAction {

	private static final long serialVersionUID = -7078569097901270563L;
	@Autowired
	private DeptUserService deptUserService;
	@Autowired
	private RoleService roleService;
//	@Autowired
//	private RoleUserService roleUserService;
	
	
	@RequestMapping("/toUserRole.do")
	public String toUserRole(){
		return "system/userRole";
	}
	
	@RequestMapping("/toUserRole_user.do")
	public String toUserRole_user(Long deptId){
		Page page=null;
		if(deptId != null){
			User user=new User();
			page=deptUserService.getUsersByDeptId(deptId,user,getPageIndex(), getPageSize());
			List<Role> list = roleService.getRoles(deptId);
			putInRequest("page", page,"list",list,"deptId",deptId);
		}
		return "system/userRole_user";
	}
	
	@RequestMapping("/getRolesByUser.do")
	public @ResponseBody void getUserByRole(Long userId){
		List<Role> list = roleService.getRoleUserByParam(userId, null);
		JSONArray ja= JsonUtil.toJSONArray(list);
		outJsonSuccess(ja.toString());
	}
	
//	@RequestMapping("/saveUserRole.do")
//	public @ResponseBody void saveUserRole(Long[] del_,Long[] new_,Long userId){
//		roleUserService.saveUserRole(new_, del_, userId);
//		Long deptId=null;
//		if(del_!=null && del_.length>0)deptId=roleUserService.getRoleByUserRoleId(del_[0])[1];
//		if(new_!=null&&new_.length>0)deptId=roleService.getRoleById(new_[0]).getDeptId();
//		if(deptId!=null)
//			clearCache(userId, deptId);
//		outJsonSuccess();
//	}
	/**
	 * 清除缓存
	 */
	private void clearCache(Long userId,Long deptId){
		RedisClientUtil.delkeyObject(ICacheConstants.CACHE_AUTH_USER_ROLE+deptId+"#"+userId);
	}
}
